package IO;



import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZC_zc on 2016/11/28.
 */
public class scanFiles {
    public static void main(String[] args) throws IOException {
        String fileName="F:";
        List<String> pathNames=getPathName(fileName);

        String targetName="D:"+File.separator+"test"+File.separator+"scan.txt";

        FileOutputStream fileOutputStream=new FileOutputStream(targetName);

        OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream,"utf-8");

        BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);

        bufferedWriter.write(pathNames.toString());

        bufferedWriter.close();

        for (String pathName:pathNames){
            System.out.println(pathName);
        }
    }

    public static List<String> getPathName(String fileName){

        List<String> pathNames=new ArrayList<>();

        try{
            File sourceFile=new File(fileName);

            if(sourceFile.isDirectory()){

                File[] files=sourceFile.listFiles();
                if (files==null){
                    pathNames.add("-----------------"+sourceFile.getAbsolutePath());
                }else{
                    for (File file:files){

                        if(file.isDirectory())
                        {
                            pathNames.add(file.getAbsolutePath());

                            pathNames.addAll(getPathName(file.toString()));
                        }else{
                            pathNames.add(file.getAbsolutePath());
                        }
                    }
                }


            }else{
                pathNames.add(sourceFile.getName());
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        return pathNames;
    }
//    @Test
    public void test(){

        String fileName="E:";
        File sourceFile=new File(fileName);
//        File[] files=null;
        File[] files=sourceFile.listFiles();

        for (File file:files){
            System.out.println(file.getName());
        }
    }

//    @Test
    public void test2(){
        List list=new ArrayList();
        list.addAll(null);
        for (Object obj:list){
            System.out.println(obj);
        }
        System.out.println();
    }
}
